// ====== Computer Graphics Experiment #1 ======
// |     Bresenham's Line Drawing Algorithm    |
// |                                           |
// =============================================
//
// Requirement:
// Implement a general Bresenham's Line Drawing Algorithm.
// Only use GL_POINTS drawing mode. 
// Do not use OpenGL line drawing capability.


//#include <windows.h> // On Linux, it doesnot need
#include <GL/glut.h>
#include <math.h>

#define PI 3.14159265359
int width, height;

void OneSection(int x, int y, int dx, int dy, int forend, int sign_x, int sign_y) {
	int p = 2*dy - dx;
	glBegin(GL_POINTS);
		for(int i=0; i<forend; ++i) {
			glVertex2i(x, y);

			if (sign_x > 0)
				x = x + 1;
			else
				x = x - 1;

			if (p < 0)
				p += 2*dy;
			else {
				if(sign_y > 0)
					y += 1;
				else
					y -= 1;

				p += 2*dy-2*dx;
			}
		}
	glEnd();
}

void AnotherSection(int x, int y, int dx, int dy, int forend, int sign_x, int sign_y) {
	int p = 2*dx - dy;
	glBegin(GL_POINTS);
		for(int i=0; i<forend; ++i) {
			glVertex2i(x, y);

			if (sign_y > 0)
				y += 1;
			else
				y -= 1;

			if (p < 0)
				p += 2*dx;
			else {
				if(sign_x > 0)
					x += 1;
				else
					x -= 1;

				p += 2*dx-2*dy;
			}
		}
	glEnd();
}


// Bresenham line algorithm
void MyLine(int xs, int ys, int xe, int ye)
{
	// Write your code here
	int dx = xe - xs;
	int dy = ye - ys;

	int m = fabs(dy/(double)dx);
	// |m|<1
	if(m < 1) {
		// 象限1
    	int end = fabs(dx);
		if(dx>=0 && dy>=0)
			OneSection(xs, ys, dx, dy, end, 1, 1);
		// 2
		else if(dx<=0 && dy>=0)
			OneSection(xs, ys, -dx, dy, end, -1, 1);
		// 4
		else if(dx>=0 && dy<=0)
			OneSection(xs, ys, dx, -dy, end, 1, -1);
		// 3
		else if(dx<=0 && dy<=0)
			OneSection(xs, ys, -dx, -dy, end, -1, -1);
	} else {
		// m >= 1
		int end = fabs(dy);
        // 象限1
        if(dx>=0 && dy>=0)
            AnotherSection(xs, ys, dx, dy, end, 1, 1); 
        // 2
        else if(dx<=0 && dy>=0)
            AnotherSection(xs, ys, -dx, dy, end, -1, 1); 
        // 4
        else if(dx>=0 && dy<=0)
            AnotherSection(xs, ys, dx, -dy, end, 1, -1);
        // 3
        else if(dx<=0 && dy<=0)
            AnotherSection(xs, ys, -dx, -dy, end, -1, -1);
	}	
}

// Initialization function
void init(void)
{
	glClearColor (0.0, 0.0, 0.0, 0.0);
}

// Display callback function
void display(void)
{
	int i, x0, y0, x, y;
	double a;

	glClear (GL_COLOR_BUFFER_BIT);

	x0=width/2;
	y0=height/2;

	glColor3f(1.0, 1.0, 1.0);

	// Draw lines
	for (i=0; i<360; i+=15)
	{
		a=(double)i/180.0*PI;
		x=0.45*width*cos(a);
		y=0.45*height*sin(a);
		if (i==0 || i==180) y=0;
		if (i==90 || i==270) x=0;
		MyLine(x0, y0, x0+x, y0+y);
	}

	glFlush();
}

// Reshape callback function
void reshape(int w, int h)
{
	// Record width and height of program window
	width=w;
	height=h;

	// Set clipping window and viewport equal to
	// view area of program window
	glViewport(0, 0, w, h);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluOrtho2D (0.0, w, 0.0, h);
}

// Keyboard callback function
void keyboard(unsigned char key, int x, int y)
{
	switch (key) {
		case 27:
			exit(0);
	
	}
}

// Main program entrance
int main(int argc, char* argv[])
{
	// Create window
	glutInit(&argc, argv);
	glutInitDisplayMode (GLUT_RGB);
	glutInitWindowSize(500, 500);
	glutCreateWindow("Lines");

	// Initialization
	init();

	// Define callback functions
	glutReshapeFunc(reshape);
	glutKeyboardFunc(keyboard);
	glutDisplayFunc(display);

	// Main program loop. (Event loop)
	glutMainLoop();

	return 0;
}
